package org.jivesoftware.smackx;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.ChatManagerListener;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.PacketInterceptor;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.NotFilter;
import org.jivesoftware.smack.filter.PacketExtensionFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.PacketExtension;
import org.jivesoftware.smack.util.collections.ReferenceMap;
import org.jivesoftware.smackx.packet.ChatStateExtension;

public class ChatStateManager
{
  private static final Map<Connection, ChatStateManager> managers = new WeakHashMap();
  private static final PacketFilter filter = new NotFilter(new PacketExtensionFilter("http://jabber.org/protocol/chatstates"));
  private final Connection connection;
  private final OutgoingMessageInterceptor outgoingInterceptor = new OutgoingMessageInterceptor(null);
  private final IncomingMessageInterceptor incomingInterceptor = new IncomingMessageInterceptor(null);
  private final Map<Chat, ChatState> chatStates = new ReferenceMap(2, 0);

  public static ChatStateManager getInstance(Connection paramConnection)
  {
    if (paramConnection == null)
      return null;
    synchronized (managers)
    {
      ChatStateManager localChatStateManager = (ChatStateManager)managers.get(paramConnection);
      if (localChatStateManager == null)
      {
        localChatStateManager = new ChatStateManager(paramConnection);
        localChatStateManager.init();
        managers.put(paramConnection, localChatStateManager);
      }
      return localChatStateManager;
    }
  }

  private ChatStateManager(Connection paramConnection)
  {
    this.connection = paramConnection;
  }

  private void init()
  {
    this.connection.getChatManager().addOutgoingMessageInterceptor(this.outgoingInterceptor, filter);
    this.connection.getChatManager().addChatListener(this.incomingInterceptor);
    ServiceDiscoveryManager.getInstanceFor(this.connection).addFeature("http://jabber.org/protocol/chatstates");
  }

  public void setCurrentState(ChatState paramChatState, Chat paramChat)
    throws XMPPException
  {
    if ((paramChat == null) || (paramChatState == null))
      throw new IllegalArgumentException("Arguments cannot be null.");
    if (!updateChatState(paramChat, paramChatState))
      return;
    Message localMessage = new Message();
    ChatStateExtension localChatStateExtension = new ChatStateExtension(paramChatState);
    localMessage.addExtension(localChatStateExtension);
    paramChat.sendMessage(localMessage);
  }

  public boolean equals(Object paramObject)
  {
    if (this == paramObject)
      return true;
    if ((paramObject == null) || (getClass() != paramObject.getClass()))
      return false;
    ChatStateManager localChatStateManager = (ChatStateManager)paramObject;
    return this.connection.equals(localChatStateManager.connection);
  }

  public int hashCode()
  {
    return this.connection.hashCode();
  }

  private boolean updateChatState(Chat paramChat, ChatState paramChatState)
  {
    ChatState localChatState = (ChatState)this.chatStates.get(paramChat);
    if (localChatState != paramChatState)
    {
      this.chatStates.put(paramChat, paramChatState);
      return true;
    }
    return false;
  }

  private void fireNewChatState(Chat paramChat, ChatState paramChatState)
  {
    Iterator localIterator = paramChat.getListeners().iterator();
    while (localIterator.hasNext())
    {
      MessageListener localMessageListener = (MessageListener)localIterator.next();
      if ((localMessageListener instanceof ChatStateListener))
        ((ChatStateListener)localMessageListener).stateChanged(paramChat, paramChatState);
    }
  }

  private class IncomingMessageInterceptor
    implements ChatManagerListener, MessageListener
  {
    private IncomingMessageInterceptor()
    {
    }

    public void chatCreated(Chat paramChat, boolean paramBoolean)
    {
      paramChat.addMessageListener(this);
    }

    public void processMessage(Chat paramChat, Message paramMessage)
    {
      PacketExtension localPacketExtension = paramMessage.getExtension("http://jabber.org/protocol/chatstates");
      if (localPacketExtension == null)
        return;
      ChatState localChatState;
      try
      {
        localChatState = ChatState.valueOf(localPacketExtension.getElementName());
      }
      catch (Exception localException)
      {
        return;
      }
      ChatStateManager.this.fireNewChatState(paramChat, localChatState);
    }
  }

  private class OutgoingMessageInterceptor
    implements PacketInterceptor
  {
    private OutgoingMessageInterceptor()
    {
    }

    public void interceptPacket(Packet paramPacket)
    {
      Message localMessage = (Message)paramPacket;
      Chat localChat = ChatStateManager.this.connection.getChatManager().getThreadChat(localMessage.getThread());
      if (localChat == null)
        return;
      if (ChatStateManager.this.updateChatState(localChat, ChatState.active))
        localMessage.addExtension(new ChatStateExtension(ChatState.active));
    }
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smackx.ChatStateManager
 * JD-Core Version:    0.6.1
 */